package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.Result;
import com.hl.sfzy.entity.SysArticle;
import com.hl.sfzy.service.SysArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SysArticleController {
    @Autowired
    private SysArticleService sysArticleService;

    /**
     * 新增
     */
    @PostMapping("/sysArticle")
    public Result add(@RequestBody SysArticle sysArticle) throws Exception {
        sysArticleService.add(sysArticle);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/sysArticle")
    public Result edit(@RequestBody SysArticle sysArticle) {
        sysArticleService.edit(sysArticle);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/sysArticle/{id}")
    public Result remove(@PathVariable Integer id) {
        sysArticleService.remove(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/sysArticle")
    public Result removeBatch(Integer[] ids) {
        sysArticleService.removeBatch(ids);
        return Result.success();
    }
    /*
    * 查询所有
    * */
    @GetMapping("/sysArticle")
    public Result find(SysArticle sysArticle) {
        List<SysArticle> list = sysArticleService.find(sysArticle);
        return Result.success(list);
    }

    /**
     * 单条查询
     */
    @GetMapping("/sysArticle/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(sysArticleService.findById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/sysArticle/page")
    public Result findPage(
            @RequestParam(value = CommonConstant.PAGE_NUM,required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(value = CommonConstant.PAGE_SIZE,required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            SysArticle sysArticle) {
        return Result.success(sysArticleService.findPage(pageNum, pageSize,sysArticle));
    }
}